/*index页面的函数，获取用户和渲染页面功能*/
function getUser() {
    if (window.localStorage) {
        // checkUser();
        // alert('This browser supports localStorage');
    } else {
        alert('该浏览器版本较低，为了更好的使用，请更换浏览器再试！');
    }

    $.ajax({
        type: "GET",
        url: "http://115.29.136.190:8080/help/usr/user/getUserStatus.do" + "?" + document.cookie,
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        success: function(result) {
            if (result.status == 1) {
                var temp = result.body;
                console.log(temp)
                localStorage.name = temp.loginName;
                // user.loginName = temp.loginName;
                localStorage.roleId = temp.role;
                localStorage.deptId = temp.deptId;
                /*开始执行vue数据渲染*/
                layout.init();
            } else {

            }
        }
    });
}
getUser();

var layout = new Vue({
    el: '#layout-container',
    data: {
        url: './subform-zhuguan.html',
        li: {
            'Zhuguan': false,
            'Buzhu': true,
            'Dept': false,
            'Month': false
        },
        userDept: localStorage.deptId,
        userName: '',
        deptIDshow: false,
        admin: true,
        system: true,
        admin_s: true,
        dept: true,
        dept_s: true,
        stu: true,
    },
    methods: {
        init: function() {
            var _self = this;
            // console.log(_global.user);
            console.log("name" + localStorage.name);
            console.log("roleId" + localStorage.roleId);
            _self.userName = localStorage.name;
            _self.getRole();
        },
        /*实现iframe跳转*/
        ijump: function(str, info) {
            var _self = this;
            console.log(info);
            _self.li = {
                'Zhuguan': false,
                'Buzhu': false,
                'Dept': false,
                'Month': false
            };
            _self.li[info] = true;
            console.log(_self.li);
            _self.url = str;
        },
        getRole: function() {
            var _self = this;
            var roleId = localStorage.roleId;
            if (roleId == 0) {
                /*管理员*/
                _self.system = false;
                _self.admin = false;
                _self.admin_s = false;

                _self.dept = true;
                _self.dept_s = true;
                _self.stu = true;

                // _self.userDept = "管理员";
                _self.ijump('./subform-shenhe.html', 'Shenhe')
            } else if (roleId == 1) {
                /*部门用户*/
                _self.admin = true;
                _self.admin_s = true;
                _self.system = true;

                _self.dept = false;
                _self.dept_s = false;
                _self.stu = true;
                _self.ijump('./subform-buzhu.html', 'Buzhu');
                _self.deptIDshow = true;
                _self.userDept = localStorage.deptId;
            } else if (roleId == 2) {
                /*学生用户*/
                _self.admin = true;
                _self.dept = true;
                _self.stu = false;
                _self.url = "./subform-zhuguan.html";
            }
        },
        getCookie: function(c_name) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=")
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1
                    c_end = document.cookie.indexOf(";", c_start)
                    if (c_end == -1) c_end = document.cookie.length
                    return unescape(document.cookie.substring(c_start, c_end))
                }
            }
            return ""
        },
        logout: function() {
            var _self = this;
            $.ajax({
                type: "POST",
                url: "http://115.29.136.190:8080/help/usr/user/shiroLogout.do",
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function(result) {
                    if (result.status == 1) {
                        // _self.setCookie('userName', result.body.loginName, 30);
                        // _self.setCookie('role', result.body.role, 30);
                        // document.cookie = document.cookie + ';role' + '=' + result.body.role;
                        console.log(document.cookie)
                        alert(result.message);
                        _self.delAllCookie();
                        localStorage = '';
                        window.location.replace("./login.html");
                    } else {
                        alert("logout")
                        alert(result.message);
                    }

                }
            });
        },
        delAllCookie: function() {
            var myDate = new Date();
            myDate.setTime(-1000); //设置时间    
            var data = document.cookie;
            var dataArray = data.split("; ");
            for (var i = 0; i < dataArray.length; i++) {
                var varName = dataArray[i].split("=");
                document.cookie = varName[0] + "=''; expires=" + myDate.toGMTString();
            }

        },
        hide: function(str) {
                console.log(str);
                var _self = this;
                switch (str) {
                    case "system":
                        if (_self.system) {
                            _self.system = false
                        } else {
                            _self.system = true
                        }
                        break;
                    case "admin_s":
                        if (_self.admin_s) {
                            _self.admin_s = false
                        } else {
                            _self.admin_s = true
                        }
                        break;
                    case "dept_s":
                        if (_self.dept_s) {
                            _self.dept_s = false
                        } else {
                            _self.dept_s = true
                        }
                        break;
                }
            }
            /*实现class的绑定*/
    }
});